<?php

namespace Mobile\Controller;

/**
 * 房态接口
 * @modify zhangzhen 2017-03-08 创建
 */
class ShareController extends ParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();
    }
    /**
     * @description 分享房卡
     * @modify zhangzhen 2017-03-08
     * @modify wenhui  2017-06-16 支持国际号码校验
     */
    public function getCardLink() {
        $params = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'mobile' => 'notnull,notempty,mobile',
            'orderRoomId' => 'notnull,notempty,id',
            'isLink' => 'notnull,notempty',
        );
        // v2.0.11的参数校验 支持国际号码校验
        $version = getVersion(array('2.0.11'));
        $areaCode = isset($params['areaCode']) ? $params['areaCode'] : '86';
        if ($version >= '2.0.11') {
            $validateRule['mobile'] = 'notnull,notempty';
            $this->ajaxValidateReturn($this->validateUtil, $params['mobile'], 'globalMobile',
                        array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                    ));
        }

        $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule);
        //检验账号
        $userId = $params['userId'];
        $token = $params['token'];
        $this->checkMobileToken($userId, $token);

        $hotelId = $params['hotel'];
        $mobile = $params['mobile'];
        $orderRoomId = $params['orderRoomId'];
        $userRoomModel = D('UserRoom');
        $userRoomInfos = $userRoomModel->getInfosByOrderRoom($orderRoomId);
        if(empty($userRoomInfos)){
            returnJson(403, 506);
        }
        $orderRoomModel = D('OrderRoom');
        $orderRoomInfos = $orderRoomModel->getInfo($orderRoomId);
        if($orderRoomInfos[0]['status'] == 3 || $orderRoomInfos[0]['status'] == 4){
            returnJson(403, 506);
        }
        $roomInfoModel = D('RoomInfo');
        $roomInfo = $roomInfoModel->getInfo($orderRoomInfos[0]['roominfo_id']);
        if($roomInfo['roomfrom'] != 1 && $roomInfo['roomfrom'] != 3){
            returnJson(403, 506);
        }

        $startTime = $userRoomInfos[0]['startdate'];
        $endTime = $userRoomInfos[0]['enddate'];

        $isLink = $params['isLink'];
        $BookCardLogic = A('Common/BookCard', 'Logic');
        $card = $BookCardLogic->shareCard($hotelId, $orderRoomId, $mobile, $isLink, $areaCode);

        returnJson(200, 200, [
                'card' => $card,
                'startTime' => $startTime,
                'endTime' => $endTime,
            ]);

    }
    /**
     * @description 邮件分享房卡
     * @modify zhangzhen 2017-03-09
     */
    public function sendCardmail() {
        $params = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'email' => 'notnull,notempty,email',
            'content' => 'notnull,notempty',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        $userId = $params['userId'];
        $token = $params['token'];
        $this->checkMobileToken($userId, $token);
        $email = $params['email'];
        $content = $params['content'];
        $emailUtil = new \Org\Util\Email();
        $result = $emailUtil->shareCard($email,$content);
        returnJson(200, 200, 'success');
    }
    
}
